package com.mall.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.api.SmsHomeRecommendSubjectApi;
import com.mall.pageResult.PageResult;
import com.mall.pojo.SmsHomeRecommendProduct;
import com.mall.pojo.SmsHomeRecommendSubject;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class HomeRecommendSubjectService {

    @DubboReference
    private SmsHomeRecommendSubjectApi homeRecommendSubjectApi;

    /**
     * 分页查询首页推荐专题
     *
     * @param pageNum
     * @param pageSize
     * @param subjectName
     * @param recommendStatus
     * @return
     */
    public PageResult<SmsHomeRecommendSubject> list(Integer pageNum, Integer pageSize, String subjectName, Integer recommendStatus) {
        //查询推荐专题分页信息
        Page<SmsHomeRecommendSubject> page = homeRecommendSubjectApi.list(pageNum, pageSize, subjectName, recommendStatus);
        //组装返回结果数据
        PageResult<SmsHomeRecommendSubject> result = new PageResult<>();
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setList(page.getRecords());
        result.setTotal(Convert.toInt(page.getTotal()));
        result.setTotalPage(Convert.toInt(page.getPages()));
        return result;
    }

    /**
     * 批量修改首页推荐专题状态
     * @param ids
     * @param recommendStatus
     * @return
     */
    public Integer updateRecommendStatus(Long[] ids, Integer recommendStatus) {
        //根据一组id查询推荐专题信息
        List<SmsHomeRecommendSubject> subjectList = homeRecommendSubjectApi.findProductByIds(ids);
        //修改这组推荐专题状态
        subjectList = subjectList.stream().map(subject->{
            subject.setRecommendStatus(recommendStatus);
            return subject;
        }).collect(Collectors.toList());
        //批量修改推荐专题状态
        Integer count = 0;
        for (SmsHomeRecommendSubject subject : subjectList) {
            homeRecommendSubjectApi.updateSubject(subject);
            count++;
        }

        return count;
    }

    /**
     * 添加首页推荐专题
     * @param homeRecommendSubjectList
     * @return
     */
    public Integer create(List<SmsHomeRecommendSubject> homeRecommendSubjectList) {
        Integer count = 0;
        //判断专题是否已经在推荐列表
        for (SmsHomeRecommendSubject subject : homeRecommendSubjectList) {
            Long subjectId = subject.getSubjectId();
            //根据专题id查询推荐专题信息
            SmsHomeRecommendSubject homeRecommendSubject = homeRecommendSubjectApi.findSubjectBySubjectId(subjectId);
            if (ObjectUtil.isNull(homeRecommendSubject)){
                //添加推荐专题
                //设置默认排序和推荐状态
                subject.setSort(0);
                subject.setRecommendStatus(1);
                homeRecommendSubjectApi.create(subject);
                count++;
            }
        }
        return count;
    }

    /**
     * 修改首页推荐专题排序
     * @param id
     * @param sort
     * @return
     */
    public Integer updateSort(Integer id, Integer sort) {
        //sort为null给个默认值0
        if (ObjectUtil.isNull(sort)){
            sort = 0;
        }
        //根据id查询推荐专题信息
        SmsHomeRecommendSubject subject = homeRecommendSubjectApi.findSubjectById(id);
        subject.setSort(sort);
        Integer count = homeRecommendSubjectApi.updateSubject(subject);
        return count;
    }

    /**
     * 批量删除首页推荐专题
     * @param ids
     * @return
     */
    public Integer delete(Long[] ids) {
        Integer count = homeRecommendSubjectApi.deleteByIds(ids);
        return count;
    }
}
